package com.lkd.handler;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lkd.business.MsgHandler;
import com.lkd.config.TopicConfig;
import com.lkd.contract.VendoutContract;
import com.lkd.contract.VendoutData;
import com.lkd.emq.MqttProducer;
import com.lkd.emq.Topic;
import com.lkd.entity.ChannelEntity;
import com.lkd.entity.SkuEntity;
import com.lkd.entity.VendoutRunningEntity;
import com.lkd.exception.LogicException;
import com.lkd.service.ChannelService;
import com.lkd.service.SkuService;
import com.lkd.service.VendoutRunningService;
import com.lkd.utils.JsonUtil;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
@Topic(TopicConfig.VMS_VENDOUT_TOPIC)
public class VendOutHandler implements MsgHandler {

    @Autowired
    private ChannelService channelService;

    @Autowired
    private MqttProducer mqttProducer;

    @Autowired
    private SkuService skuService;

    @Autowired
    private VendoutRunningService vendoutRunningService;

    @Qualifier("getRedisson")
    @Autowired
    private RedissonClient redissonClient;

    @Override
    public void process(String jsonMsg) throws IOException {
        VendoutContract vendoutContract = JsonUtil.getByJson(jsonMsg, VendoutContract.class);
        VendoutData vendoutData = vendoutContract.getVendoutData();
        long skuId = vendoutData.getSkuId();
        String innerCode = vendoutContract.getInnerCode();
        //取出该对应设备和商品的并有库存的货道
        List<ChannelEntity> list = channelService.list(new LambdaQueryWrapper<ChannelEntity>().eq(ChannelEntity::getInnerCode, innerCode).eq(ChannelEntity::getSkuId, skuId).gt(ChannelEntity::getCurrentCapacity,0));
        if(null == list&& list.size()==0){
            throw new LogicException("库存不足,请及时补货");
        }
        ChannelEntity channelEntity = list.get(0);
        channelEntity.setCurrentCapacity(channelEntity.getCurrentCapacity()-1);
        channelService.updateById(channelEntity);
        //出货流水封装
        VendoutRunningEntity vendoutRunningEntity = getVendoutRunningEntity(vendoutData, skuId, channelEntity);
        vendoutRunningService.save(vendoutRunningEntity);
        //发消息到设备出货
        vendoutData.setChannelCode(channelEntity.getChannelCode());
        vendoutContract.setVendoutData(vendoutData);
        String msg = JSON.toJSONString(vendoutContract);
        mqttProducer.send(TopicConfig.getVendoutTopic(vendoutContract.getInnerCode()),msg);
        RLock lock = redissonClient.getLock(String.format(vendoutContract.getInnerCode()));
        lock.unlock();
    }

    //获取流水封装类
    private VendoutRunningEntity getVendoutRunningEntity(VendoutData vendoutData, long skuId, ChannelEntity channelEntity) {
        VendoutRunningEntity vendoutRunningEntity = new VendoutRunningEntity();
        vendoutRunningEntity.setInnerCode(channelEntity.getInnerCode());
        vendoutRunningEntity.setOrderNo(vendoutData.getOrderNo());
        vendoutRunningEntity.setSkuId(skuId);
        vendoutRunningEntity.setStatus(false);
        vendoutRunningEntity.setPrice(channelEntity.getPrice());
        SkuEntity skuEntity = skuService.getById(skuId);
        vendoutRunningEntity.setSkuName(skuEntity.getSkuName());
        return vendoutRunningEntity;
    }
}
